package client.controller;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JOptionPane;

import serveur.util.Constantes;

import commons.downloader.ClientDownloader;
import commons.downloader.Downloader;

import client.vue.ChoixJeuxVue;
import client.vue.ConnexionVue;
import client.vue.Fenetre;
import client.vue.JeuxVue;
import client.vue.ResultatsVue;
/**
 * 
 * @author michael
 *
 */
public class ClientThread {
	Socket s;
	BufferedWriter writeSocket;
	BufferedReader readSocket;
	String send="";
	Fenetre fenetre;
	boolean isPlaying=false;
	
	public ClientThread() throws IOException{
		this.s = connexion();
		this.writeSocket = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
		this.readSocket = new BufferedReader(new InputStreamReader(s.getInputStream()));
		clientRun.start();
	} 
	
	//creation de socket
	public Socket connexion() {
		Socket s = null;
		try {
			while (s==null){
				s = new Socket("192.168.1.24", 1032);
			}
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return s;
	}
	
	//deconnection de la socket
	public void deconnection(Socket s){
		try {
			s.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**Thread client**/
		Thread clientRun = new Thread(){
			public void run() {
				/**Identification ou Inscription**/	
				String action,value = null;
				try {
					value = readSocket.readLine();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				action = value.split(":")[0];
				
				System.out.println("action "+action);
				
				if (action.equals("ErreurAuthentification")){
					ConnexionVue vue = (ConnexionVue)fenetre.getPanneau();
					vue.setPseudo("Erreur d'authentification");
				}else if(action.equals("AuthentificationOk")){
					fenetre.setPanneau(new ChoixJeuxVue(fenetre));
					//synchronisation();
				}else if(action.equals("InscriptionOk")){
					JOptionPane.showMessageDialog(null, "Vous �te maintenant enregistr�","F�licitation !!",JOptionPane.WARNING_MESSAGE);	
					fenetre.setPanneau(new ChoixJeuxVue(fenetre));
					//synchronisation();
				}
				
				while(true) {
					try {
						value = readSocket.readLine();
						System.out.println("client : all flux "+value);
						action = value.split(":")[0];
						if(action.equals("PartieEnCours")){
							ChoixJeuxVue form = (ChoixJeuxVue)fenetre.getPanneau();
							form.setPartiEnCours(value.split(":")[1]);
						}
						if(action.equals("Jouer")){
							isPlaying = true;
							System.out.println("image a afficher "+value);
							value = value.split(":")[1];
							String id = value.split("-")[0];
							String reponse = value.split("-")[1];
							System.out.println("client:reseption du flux");
							fenetre.setPanneau(new JeuxVue(fenetre));
							JeuxVue form = (JeuxVue) fenetre.getPanneau();
							form.setReponse(reponse);
							form.getImage().setImageById(id);
						}else if(action.equals("PartieFinie")){
							fenetre.setPanneau(new ResultatsVue(fenetre));
							//JeuxVue form = (JeuxVue) fenetre.getPanneau();
							//form.setReponseJoueur("La partie est finie !!");
						}else if(action.equals("Score")){
							ResultatsVue form = (ResultatsVue) fenetre.getPanneau();
							value = value.split(";")[1];
							//form.addJoueurIntoListeScore(value.split(",")[0],value.split(",")[1]);
							
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			}
		};

	public void synchronisation(){
		/**Synchronisation des images**/
		Downloader cd = new ClientDownloader("src/serveur/image/");
		try {
			cd.download(s);
		} catch (Exception e1) {
			e1.printStackTrace();
		} 
	}

	public void writeSocket(String value){
		send = value;
		try {
			writeSocket.write(send+"\n");
			writeSocket.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("methode writeSocket21321 "+send);		
	}
	
	public void writeSocket(String value,Fenetre fenetre){
		this.fenetre=fenetre;
		send = value;
		try {
			writeSocket.write(send+"\n");
			writeSocket.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("methode writeSocket "+send);		
	}
}


